<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Feedback.php
 * 意见反馈
 * @author  :  Skiychan <dev@skiy.net>
 * @link    :  http://www.zzzzy.com
 * @created :  2016/2/1
 * @modified:
 * @version :  0.0.1
 */

require(APPPATH.'libraries/Web_Controller.php');
class Feedback extends Web_Controller {
    public function __construct() {
        parent::__construct();
        #文件上传的路径
        $this->load->service("feedback_service");
    }

    /**
     * 提交意见反馈
     */
    public function index_post() {
        $this->load->service('login_service');
        #如果已登陆则添加用户信息
        if ($this->login_service->is_login()) {
            $contact['uid'] = $this->login_service->get_user_info('id');
            $nickname = $this->login_service->get_user_info('nickname');
            $login_email = $this->login_service->get_user_info('email');
            if (! empty($nickname)) $contact['username'] = $nickname;
        }

        $content = trim($this->input->post('content', true));
        if (empty($content))
            return $this->common_response(1003);

        $qq = $this->input->post('qq', true);
        if (! empty($qq))
            $contact['qq'] = $qq;
        $email = trim($this->input->post('email', true));
        if (empty($email) && (! empty($login_email)))
            $email = $login_email;
        if (! empty($email))
            $contact['email'] = $email;

        /*
        $title = trim($this->input->post('contact', true)) ?:
            sprintf($this->lang->line('feedback'),date('Ymd'));*/

        $title = trim($this->input->post('title', true));
        $content = trim($this->input->post('content', true));
        #图片附件
        $file_path = $this->input->post('file', true);

        if (is_array($file_path))
            $file_path = implode("|",$file_path);

        $resp = $this->feedback_service->doadd($content, $contact, $title, $file_path);

        if (empty($resp))
            return $this->common_response($this->feedback_service);

        return $this->common_response(0);
    }

    /**
     * 上传图片
     */
    public function image_post() {
        $files = $this->feedback_service->upload("file");
        if (! $files)
            return $this->common_response($this->feedback_service);

        return $this->common_response(0, "", array("data" => $files));
    }

    /**
     * 删除图片
     */
    public function image_delete() {
        $filename = $this->delete('file');
        $filepath = $this->feedback_service->file_dir.$filename;

        #文件存在,删除文件
        if (file_exists($filepath)) {
            if (! unlink($filepath))
                return $this->common_response(1001);
        }

        return $this->common_response(0);
    }
}